package com.dkm.ec.sale.repository.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.gl.persistence.entity.EavAttributePo;
import com.dkm.biz.gl.repository.EavAttributeRepository;
import com.dkm.biz.repository.AbstractRepository;
import com.dkm.ec.sale.domain.SaleConstants;
import com.dkm.ec.sale.domain.SalePackageItem;
import com.dkm.ec.sale.domain.SalePrice;
import com.dkm.ec.sale.domain.SaleSku;
import com.dkm.ec.sale.persistence.dao.SalePackageItemDao;
import com.dkm.ec.sale.persistence.dao.SalePackageItemQueryDao;
import com.dkm.ec.sale.persistence.dao.SaleSkuDao;
import com.dkm.ec.sale.persistence.dao.SaleSkuQueryDao;
import com.dkm.ec.sale.persistence.entity.SalePackageItemPo;
import com.dkm.ec.sale.persistence.entity.SalePricePo;
import com.dkm.ec.sale.persistence.entity.SaleSkuPo;
import com.dkm.ec.sale.repository.SalePackageItemRepository;
import com.dkm.ec.sale.repository.SalePriceRepository;
import com.dkm.ec.sale.repository.SaleSkuRepository;

/**
 * 对象功能:产品套餐构件 Repository接口的实现类 开发公司:SKG.COM 开发人员:YuanJiaQiu 创建时间:2015-08-13
 * 10:52:54
 */
@Repository
public class SalePackageItemRepositoryImpl extends
		AbstractRepository<String, SalePackageItemPo, SalePackageItem>
		implements SalePackageItemRepository {

	@Resource
	private SalePackageItemQueryDao salePackageItemQueryDao;
	@Resource
	private SaleSkuDao saleSkuDao;
	@Resource
	private SaleSkuQueryDao saleSkuQueryDao;
	@Resource
	private SalePackageItemDao salePackageItemDao;
	@Resource
	private SaleSkuRepository saleSkuRepository;
	@Resource
	private SalePriceRepository salePriceRepository;
	@Resource 
	private EavAttributeRepository eavAttributeRepository;

	public SalePackageItem newInstance() {
		SalePackageItemPo po = new SalePackageItemPo();
		SalePackageItem salePackageItem = SpringHelper
				.getBean(SalePackageItem.class);
		salePackageItem.setData(po);
		return salePackageItem;
	}

	public SalePackageItem newInstance(SalePackageItemPo po) {
		SalePackageItem salePackageItem = SpringHelper
				.getBean(SalePackageItem.class);
		salePackageItem.setData(po);
		return salePackageItem;
	}

	@Override
	protected IQueryDao<String, SalePackageItemPo> getQueryDao() {
		return salePackageItemQueryDao;
	}

	@Override
	public List<SalePackageItemPo> findBySkuId(String skuId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuilder sb = new StringBuilder(" sku_id_ = '" + skuId + "'");
		paramMap.put("whereSql", sb.toString());
		return salePackageItemQueryDao.findAll(paramMap);
	}

	@Override
	public List<SalePackageItemPo> findBySkuIdAndItemSkuId(String skuId,
			String itemSkuId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuilder sb = new StringBuilder(" sku_id_ = '" + skuId + "'");
		sb.append(" and item_sku_id_ = '" + itemSkuId + "'");
		paramMap.put("whereSql", sb.toString());
		return salePackageItemQueryDao.findAll(paramMap);
	}

	@Override
	public List<SalePackageItemPo> findByProdId(String prodId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuilder sb = new StringBuilder(" prod_id_ = '" + prodId + "'");
		paramMap.put("whereSql", sb.toString());
		return salePackageItemQueryDao.findAll(paramMap);
	}

	@Override
	public void deletePackage(String id) {

		SalePackageItemPo salePackageItemPo = salePackageItemQueryDao.get(id);
		SaleSkuPo saleSkuPo = saleSkuQueryDao.get(salePackageItemPo.getSkuId());
		saleSkuPo.setStatus("delete");
		// 改变被删除套餐对应的sku状态
		saleSkuDao.update(saleSkuPo);

		// 删除套餐项目
		List<SalePackageItemPo> salePackageItemPos = findBySkuId(saleSkuPo
				.getId());
		for (SalePackageItemPo po : salePackageItemPos) {
			salePackageItemDao.delete(po.getId());
		}
		// 删除套餐sku
		salePackageItemDao.delete(id);

	}

	public void editPackage(Map<String, Object> dataMap) {

		String skuId = (String) dataMap.get("skuId");
		String salePackageItemId = (String) dataMap.get("salePackageItemId");
		String skuName = (String) dataMap.get("skuName");
		String saleEntityId = (String) dataMap.get("saleEntityId");
		String sort = (String) dataMap.get("sort");
		String status = (String) dataMap.get("status");
		String  listPrice =  (String)dataMap.get("listPrice");
		
		// 删除旧价格
		if(!StringUtils.isEmpty(skuId)){
			List<SalePricePo> salePricePos = salePriceRepository
					.findBySkuId(skuId);
			SalePrice salePrice = salePriceRepository.newInstance();
			for (SalePricePo salePricePo : salePricePos) {
				salePrice.destroy(salePricePo.getId());
			}
		}
		
		//保存价格
		Map<String, Object> paramMap= new HashMap<String, Object>();
		paramMap.put("whereSql", "id_ in( select attr_id_ from skg_eav_set_attr  where group_id_ in (  select id_ from skg_eav_set_group " +
				"where set_id_ =  (select id_ from  skg_eav_set where key_='price')))");
		List<EavAttributePo>eavAttributePos  = eavAttributeRepository.findAll(paramMap);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<SalePricePo> salePricePos = new ArrayList<SalePricePo>();
 		for(EavAttributePo eavAttributePo :eavAttributePos){
			for (Entry<String, Object> entry : dataMap.entrySet()) {
				SalePricePo salePricePo = new SalePricePo();
				String key= entry.getKey().trim();
				String value =entry.getValue().toString().trim();
					if(eavAttributePo.getKey().equals(key)){
						salePricePo.setType(key);
						salePricePo.setPrice(Double.valueOf(value));
						salePricePo.setMinNum(1);
						salePricePo.setFromTime(new Date());
						salePricePo.setMaxTotal(Integer.valueOf((String) dataMap.get(key+"MaxNum")));
						salePricePo.setProdSkuId(skuId);
						salePricePo.setSaleTotal(0);
						try {
							salePricePo.setFromTime(sdf.parse(dataMap.get(key+"OnSaleBegin").toString()));
							salePricePo.setToTime(sdf.parse(dataMap.get(key+"OnSaleEnd").toString()));
						} catch (ParseException e) {
							e.printStackTrace();
						}
						salePricePo.setStatus("on_sale");
						SalePrice salePrice = new SalePrice();
						salePricePos.add(salePricePo);
						salePrice.setData(salePricePo);
						salePrice.save();
					}			
			}
		}		
		

		// 装载领域对象
		// 是否新增
		boolean isNew = StringUtils.isEmpty(skuId) ? true : false;
		SaleSku saleSku = null;
		if (!isNew) {
			saleSku = saleSkuRepository.load(skuId);
			if (saleSku != null) {
				// 更新sku
//				List<SaleSkuPo> saleSkuPos = new ArrayList<SaleSkuPo>();
				SaleSkuPo saleSkuPo = saleSku.getData();
				saleSkuPo.setSkuName(skuName);
				saleSkuPo.setKey(skuName);
				saleSkuPo.setOpKey(skuName);
				saleSkuPo.setSeoKey(skuName);
				saleSkuPo.setAttrOpLabels(skuName);
				saleSkuPo.setStatus(status);
				if(!StringUtils.isEmpty(listPrice)){
					saleSkuPo.setListPrice(Double.valueOf(listPrice));
				}else {
					saleSkuPo.setListPrice(0.00);
				}
				saleSku.setData(saleSkuPo);
//				saleSkuPos.add(saleSkuPo);
//				saleSkuPo.setSalePricePos(salePricePos);
//				saleSku.saveSaleSkusWithAllPrice(saleSkuPos, saleEntityId);
				saleSku.update();

				// 修改套餐项
				SalePackageItem salePackageItem = load(salePackageItemId);
				SalePackageItemPo salePackageItemPo = salePackageItem.getData();
				salePackageItemPo.setSort(Integer.valueOf(sort));
				salePackageItem.update();


			}
		} else {
			// 新增sku
			saleSku = saleSkuRepository.newInstance();
			List<SaleSkuPo> saleSkuPos = new ArrayList<SaleSkuPo>();
			SaleSkuPo saleSkuPo = new SaleSkuPo();
			saleSkuPo.setSkuName(skuName);
			saleSkuPo.setSaleProdId(saleEntityId);
			saleSkuPo.setCreateType("manual");
			saleSkuPo.setSeoKey(skuName);
			saleSkuPo.setOpKey(skuName);
			saleSkuPo.setStatus("on_sale");
			saleSkuPo.setKey(skuName);
			saleSkuPo.setStatus(status);
			saleSku.setData(saleSkuPo);
			saleSkuPo.setSalePricePos(salePricePos);
			saleSkuPos.add(saleSkuPo);
			saleSku.saveSaleSkusWithAllPrice(saleSkuPos, saleEntityId);
			saleSku.save();
			skuId=saleSku.getData().getId();

			// 新增套餐项
			SalePackageItem salePackageItem = new SalePackageItem();
			SalePackageItemPo salePackageItemPo = new SalePackageItemPo();
			salePackageItemPo.setSkuId(saleSkuPo.getId());
			salePackageItemPo.setItemSkuId(saleSkuPo.getId());
			salePackageItemPo.setProdId(saleSkuPo.getSaleProdId());
			salePackageItemPo.setItemProdId(saleSkuPo.getSaleProdId());
			salePackageItemPo.setSort(Integer.valueOf(sort));
			salePackageItemPo.setItemCount(0);
			salePackageItemPo.setSort(1);
			salePackageItem.setData(salePackageItemPo);
			salePackageItem.save();
		}
	}

	@Override
	public List<SalePackageItemPo> findByItemSku(String itemSkuId) {
		List<SalePackageItemPo> salePackageItemPos = new ArrayList<SalePackageItemPo>();
		List<SalePackageItemPo> salePackageItemPos3 = new ArrayList<SalePackageItemPo>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("itemSkuId", itemSkuId);
		salePackageItemPos = salePackageItemQueryDao.findByKey("findByItemSku", paramMap);
		for(SalePackageItemPo salePackageItemPo : salePackageItemPos){
			SaleSku saleSku  = saleSkuRepository.load(salePackageItemPo.getItemSkuId());
			if(saleSku != null){
				salePackageItemPo.setSkuName(saleSku.getData().getName());
				salePackageItemPo.setSalePrice(new BigDecimal(saleSku.getData().getSalesPrice()));
				
				List<SaleSkuPo> itemSaleSkuPos = new ArrayList<SaleSkuPo>();
				List<SalePackageItemPo> salePackageItemPos2 = this.findBySkuId(salePackageItemPo.getSkuId());
				
				boolean rushFlag = false;
				
				if(salePackageItemPos2 != null && salePackageItemPos2.size() > 0){
					for(SalePackageItemPo salePackageItemPo2 : salePackageItemPos2){
						//排除套餐自己
						if(salePackageItemPo2.getSkuId().equals(salePackageItemPo2.getItemSkuId())){
							continue;
						}
						SaleSku saleSku2  = saleSkuRepository.load(salePackageItemPo2.getItemSkuId());
						if(saleSku2 != null){
							if(SaleConstants.SALE_TYPE.RUSH.equals(saleSku2.getData().getSalesType())){
								rushFlag = true;
							}
							itemSaleSkuPos.add(saleSku2.getData());
						}
					}
				}
				
				if(!rushFlag){
					salePackageItemPo.setSaleSkuPos(itemSaleSkuPos);
					salePackageItemPos3.add(salePackageItemPo);
				}
			}
		}
		return salePackageItemPos3;
	}
	

}
